#ifndef cDIRTEMPFUNC_H
#define cDIRTEMPFUNC_H

#include <string>

#if defined(_WIN32)
		std::string const dir_sep("\\");
#else
		std::string const dir_sep("/");
#endif

struct path_sep_comp
{
    bool operator () (char ch) const
    {
#if defined (_WIN32)
        return ch == ('\\') || ch == ('/');
#else
        return ch == ('/');
#endif
    }
};

struct is_empty_string
{
    bool operator () (std::string const & str) const
    {
        return str.empty();
    }
};

template <typename PathSepPred, typename Container>
static void Tool_split_into_components(
    Container & components,
    std::string const & path,
    PathSepPred is_sep = PathSepPred())
{
    std::string::const_iterator const end = path.end();
    std::string::const_iterator it = path.begin();
    while (it != end)
    {
        std::string::const_iterator sep = std::find_if(it, end, is_sep);
        components.push_back(std::string(it, sep));
        it = sep;
        if (it != end)
            ++it;
    }
}

template <typename PathSepPred, typename Container>
void Tool_expand_relative_path(Container & components, PathSepPred is_sep = PathSepPred())
{
    // Get the current working director.

    char chValue[512] = {0};
    // std::string const cwd = Tool_get_current_dir();
#ifdef _WIN32
    _getcwd(chValue, sizeof(chValue));
    
#else
    getcwd(chValue, sizeof(chValue));
#endif
    std::string const cwd(chValue);

    // Split the CWD.

    std::vector<std::string> cwd_components;

    // Use qualified call to appease IBM xlC.
    Tool_split_into_components(cwd_components, cwd, is_sep);

    // Insert the CWD components at the beginning of components.

    components.insert(components.begin(), cwd_components.begin(), cwd_components.end());
}

template <typename Cont>
void Tool_remove_empty(Cont & cont, std::size_t special)
{
    cont.erase(std::remove_if(cont.begin() + special, cont.end(), is_empty_string()), cont.end());
}


//! Join a list of items into a string.
template <typename Iterator>
inline void Tool_join(std::string & result, Iterator start, Iterator last, std::string const & sep)
{
    if (start != last)
        result = *start++;

    for (; start != last; ++start)
    {
        result += sep;
        result += *start;
    }
}

#endif